/**
 * Copyright 2014 wasu.com
 *
 *
 * Create on 2015-1-6 下午6:37:09
 */
package xj.toolkit.jms.actor;

import com.google.common.base.Preconditions;

import xj.toolkit.jms.DestinationJMSMessgeSender;
import xj.toolkit.util.ClosureEx;

/**
 * @author <a href="mailto:caoxiaojian@wasu.com>xiaojian.cao</a>
 * 
 */
public class MessageSendFunctor implements ClosureEx {

	private DestinationJMSMessgeSender messageSender = null;

	private boolean canceled = false;

	public MessageSendFunctor(DestinationJMSMessgeSender sender) {
		Preconditions.checkArgument(sender != null);

		this.messageSender = sender;
	}

	/**
	 * @see stc.skymobi.util.ClosureEx#execute(java.lang.Object[])
	 */
	@Override
	public void execute(Object... args) {
		if (!canceled) {
			for (Object obj : args) {
				messageSender.sendMessage(obj);
			}
		}
	}

	/**
	 * @return the canceled
	 */
	public boolean isCanceled() {
		return canceled;
	}

	/**
	 * @param canceled
	 *            the canceled to set
	 */
	public void setCanceled(boolean canceled) {
		this.canceled = canceled;
	}

	/**
	 * 
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();

		sb.append(this.messageSender);
		sb.append(".");

		if (this.canceled) {
			sb.append("[canceled]");
		}
		return sb.toString();
	}

}
